/* eslint-disable */

// import { CancellationToken, editor, languages, Position } from 'monaco-editor';
import { FuncMap } from './FuncMap';
// import { CommonCompletionItem, CompletionMeta } from './types';
export default class HelmTemplateCompletionProvider {
  // funcMap;
  // triggerCharacters;
  constructor(headerChar) {
    this.headerChar = headerChar;
    const CHARCODE_A_LC = 97;
    const lowercaseAlphabet = new Array(26)
      .fill(null)
      .map((v, i) => String.fromCharCode(CHARCODE_A_LC + i));

    this.triggerCharacters = [...lowercaseAlphabet, ':'];
    this.funcMap = new FuncMap();
  }

  provideCompletionItems(model, position, context, token) {
    const wordUntilPosition = model.getWordUntilPosition(position);
    if (!wordUntilPosition) {
      return {
        suggestions: [],
        incomplete: false,
      };
    }
    const line = model.getLineContent(position.lineNumber);
    if (true) {
      const suggestions = this.dotCompletionItems(line, position.column - 2)?.map((item) => {
        return item.toCompletionItem(wordUntilPosition, position);
      });
      return {
        suggestions,
        incomplete: true,
      };
    }

    // 这里是要allFuncSuggestion
    const suggestions = this.funcMap.all().map((item) => {
      return item.toCompletionItem(wordUntilPosition, position);
    });

    return {
      suggestions,
      incomplete: true,
    };
  }

  /**
   * 这个是 . 后面的自动提示
   * 比如
   * .Values
   * .Release
   * .Values.xxx
   * @param lineUntil
   */
  dotCompletionItems(lineUntil, column) {
    const lastSpace = lineUntil.slice(0, column).lastIndexOf(' ') + 1;
    const nextSpace = lineUntil.slice(column + 1).indexOf(' ') + column + 2;
    let matchingChar;
    const startIndex =
      lineUntil.slice(0, column + 1).lastIndexOf(' ') === -1
        ? 0
        : lineUntil.slice(0, column + 1).lastIndexOf(' ');
    const endIndex =
      lineUntil.indexOf(' ', column + 1) === -1
        ? lineUntil.length
        : lineUntil.indexOf(' ', column + 1);
    if (column === 0) {
      this.headChar = lineUntil.slice(1, 2);
    }
    matchingChar = this.headerChar + lineUntil.slice(startIndex + 1, endIndex);
    const matchingArr = [
      'mine',
      'repo',
      'file',
      'lang',
      'author',
      'reviewer',
      'mrstate',
      'visibility',
      'ext',
    ];
    const completionGain = matchingArr.filter((i) => i.includes(matchingChar));
    if (completionGain.length) {
      return this.funcMap.CompletionItems(completionGain);
    }
    if (lineUntil.slice(lastSpace, nextSpace).includes('lang:')) {
      const filterKey = lineUntil?.slice(lastSpace, nextSpace)?.split(':')?.pop();
      return this.funcMap?.lang(filterKey.trim());
    } else if (lineUntil.slice(lastSpace, nextSpace).includes('mine:')) {
      return this.funcMap?.mine();
    } else if (lineUntil.slice(lastSpace, nextSpace).includes('mrstate:')) {
      return this.funcMap?.mrstate();
    } else if (lineUntil.slice(lastSpace, nextSpace).includes('visibility:')) {
      return this.funcMap?.visibility();
    }
  }

  resolveCompletionItem(_item, _token) {
    return undefined;
  }

  refreshValues(values) {
    this.customKeys = values;
  }
  refreshAppstackValues(values) {
    this.appstackKeys = values;
  }
  refreshImageValues(values) {
    this.imageKeys = values;
  }
}
